﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Web;
using System.Configuration;
using System.IO;
using System.Text.RegularExpressions;

using Core.BOL;
using Core.DAL;
using Core.Extentions;
using System.Net;
using System.Web.Mvc;

namespace Core.Utils
{
    public class Mail
    {
        public static void SendMail(string to, string subject, string body, string fromAdress, string fromName)
        {
            using (MailMessage msg = new MailMessage())
            {
                msg.To.Add(new MailAddress(to));
                if (!string.IsNullOrEmpty(fromAdress) && !string.IsNullOrEmpty(fromName))
                {
                    msg.From = new MailAddress(fromAdress, fromName);
                }
                else if (!string.IsNullOrEmpty(fromAdress))
                {
                    msg.From = new MailAddress(fromAdress);
                }
                msg.Body = body;
                msg.BodyEncoding = System.Text.Encoding.GetEncoding("iso-8859-1");
                msg.IsBodyHtml = true;
                msg.Subject = subject;

                using (SmtpClient client = new SmtpClient())
                {
                    client.Send(msg);
                }
            }
        }

        public static string GetMailHTML(string url, out string title)
        {
            return GetMailHTML(url, null, out title);
        }

        public static string GetMailHTML(string url, string postDatas, out string title)
        {   
            string mailText = string.Empty;
            string pageTitle = string.Empty;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
            if (!string.IsNullOrEmpty(postDatas))
            {
                request.AllowWriteStreamBuffering = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded; charset=ASCII";
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
                {
                    writer.Write(postDatas);
                }
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response != null && response.StatusCode != HttpStatusCode.NoContent)
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    mailText = reader.ReadToEnd();
                }

                response.Close();
                request.Abort();

                pageTitle = FetchTitleFromHTML(mailText);
            }

            title = (!string.IsNullOrEmpty(pageTitle)) ? pageTitle : string.Empty;
            return mailText;
        }

        public static string FetchTitleFromHTML(string sHtml)
        {
            string regex = @"(?<=<title.*>)([\s\S]*)(?=</title>)";
            Regex ex = new Regex(regex, RegexOptions.IgnoreCase);
            return ex.Match(sHtml).Value.Trim();
        } 
    }
}
